package com.raidpoints.client;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.google.gwt.i18n.client.DateTimeFormat;

public class NCDate implements Serializable{
	public int year;
	public int month;
	public int day;
	public int hour;
	public int minute;
	public long ms;
	
	public NCDate(){
		
	}
	
	public NCDate(int year_,int month_,int day_,int hour_, int minute_){
		year = year_ - 1900; month = month_; day = day_; hour = hour_; minute = minute_;
		ms = minstamp(this);
	}
	
	public NCDate(String dt){
		String[] split = dt.split(",");
		year = Integer.parseInt(split[0]) - 1900; month = Integer.parseInt(split[1]);
		day = Integer.parseInt(split[2]); hour = Integer.parseInt(split[3]); minute = Integer.parseInt(split[4]);
		ms = minstamp(this);
	}
	
	public NCDate(Date dt){
		if(dt == null) return;
		//return DateTimeFormat.getFormat("yyyy-MM-dd HH:mm").format(object.starttime,rp.pacTime);			
		String[] split = DateTimeFormat.getFormat("yyyy,MM,dd,HH,mm").format(dt).split(",");
		year = Integer.parseInt(split[0]) - 1900; month = Integer.parseInt(split[1]);
		day = Integer.parseInt(split[2]); hour = Integer.parseInt(split[3]); minute = Integer.parseInt(split[4]);
		ms = minstamp(this);
	}
	
	public NCDate(NCDate dt){
		year = dt.year; month = dt.month; day = dt.day; hour = dt.hour; minute = dt.minute; 
		ms = minstamp(this);
	}
	
	public void AddHour(int h){
		if(hour + h < 0){
			hour = (24 + hour + h)%24;
			day--;
			if(day < 1){
				month--;
				if(month < 1){
					month = 12;
					year--;
				}
				day = getDays(month);
			}
		}
		else if(hour + h > 24){
			hour = (hour + h) % 24;
			day++;
			if(day > getDays(month)){
				day = 1;
				month++;
				if(month > 12){
					month = 1;
					year++;
				}
			}
		}
		else hour = (hour + h) % 24;
	}
	
	public void setTime(int h, int m){
		if(h < (hour-6)){
			day++;
			if(day > getDays(month)){
				day = 1;
				month++;
				if(month > 12){
					month = 1;
					year++;
				}
			}
		}
		hour = h; minute = m;
		ms = minstamp(this);
	}
	
	public int compareTo(NCDate od){
		long d = diff(od);
		if(d == 0) return 0;
		if(d < 0) return -1;
		if(d > 0) return 1;
		else return 0;
	}
	
	public long diff(NCDate od){
		return ms - od.ms;
	}
	
	public boolean sameDay(NCDate od){
		if(od.year == year && od.month == month && od.day == day) return true;
		else return false;
	}
	
	public long minstamp(NCDate od){
		long dty = 0;
		for(int i = 1; i < od.month; i++) dty += getDays(i);
		return ((od.year -1) * 525600) + (dty * 1440) + ((od.day - 1) * 1440) + (od.hour * 60) + od.minute;
	}
	
	public int getDays(int m){
		if(m == 4 || m == 6 || m  == 9 || m == 11) return 30;
		else if( m == 2){
			return 28;
		}
		else return 31;
	}
	
	public String getFullDate(){
		return getDate() + " " + getTime();
	}	
	public String getFullDate(boolean mil){
		return getDate() + " " + getTime(mil);
	}
	
	public String getDate(){
		return (year + 1900) + "-" + month + "-" + day;
	}
	
	public String getTS(){
		return (year + 1900) + "-" + (month < 10 ? "0" : "") + month + "-" + (day < 10 ? "0" : "") + day + " " + getTime() + ":00";
	}
	
	public String getTime(boolean mil){
		if(mil) return getTime();
		else{
			return ((hour % 12) == 0 ? "12" : Integer.toString(hour%12)) + ":" + (minute < 10 ? "0" : "") + minute + (hour < 12 ? "AM" : "PM");
		}
	}
	
	public String getTime(){
		return (hour < 10 ? "0" : "") + hour + ":" + (minute < 10 ? "0" : "") + minute;
	}
	


}
